package com.itcode.mapper;

import com.itcode.pojo.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    /**
     * category 表添加数据
     * @param category 文章分类
     */
    @Insert("insert into category(category_name, category_alias, create_user, create_time, update_time)" +
            "values (#{categoryName},#{categoryAlias},#{createUser},#{createTime},#{updateTime})")
    void add(Category category);

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @Select("select * from category where id = #{id}")
    Category findById(Integer id);

    /**
     * 更新分类
     * @param category
     */
    @Update("update category set category_name = #{categoryName}, category_alias = #{categoryAlias}," +
            "update_time = #{updateTime} where id = #{id}")
    void update(Category category);

    /**
     * 查找该分类下的文章数量
     * @param id 分类id
     * @return 数量
     */
    @Select("select count(0) from category c left join article a " +
            "on c.id = a.category_id where c.id = #{id} and a.id is not null ")
    Integer countArticleById(Integer id);

    /**
     * 根据id删除分类
     * @param id id
     */
    @Delete("delete from category where id = #{id}")
    void deleteById(Integer id);

    /**
     * 查询文章分类列表
     * @return
     */
    @Select("select * from category where create_user = #{id}")
    List<Category> findAll(Integer id);

    /**
     * 根据分类名称查询分类是否存在
     * @param categoryName
     * @return
     */
    @Select("select count(0) from category where category_name = #{categoryName} and create_user = #{id}")
    Integer countByName(String categoryName, Integer id);
}
